
//% color="#5ba55b" iconWidth=50 iconHeight=40
namespace RGBLED_DotMatrix { 

    //% extenralFunc
    export function getBuiltinFunc_() {
        return [
            {matrix: "00000000000000000011100000011100001110000001110000111000000111000000000000000000000000000000000000000100001000000000001111000000"},
            {matrix: "01000100001000100010100000010100000100000000100000101000000101000100010000100010000000000000000000000000000000000000001111000000"},
            {matrix: "00000000000000000000000000000000000000000000110000110000000011000000000000000000000000000000000000010000000010000000111111110000"},
            {matrix: "00110000000011000100100000010010010010000001001001001000000100100011000000001100000000000000000000001000000000000000111111110000"},
            {matrix: "00000000000000000011100000010100001010000000100000111000000101000000000000000000000000000000000000010000000010000000111111110000"}
        ]
    }
    //% extenralFunc55
    export function getBuiltinFunc_88() {
        return [
            {matrix: "0001100000111100011111101101101110011001000110000001100000011000"},
            {matrix: "0001100000011000000110001001100111011011011111100011110000011000"},
            {matrix: "0001100000110000011000001111111111111111011000000011000000011000"},
            {matrix: "0001100000001100000001101111111111111111000001100000110000011000"},
            {matrix: "0000000001100110111111111111111111111111011111100011110000011000"}
        ]
    }
    
    function binaryToHex(binaryStr) {
    // 去掉 "0B" 前缀
    binaryStr = binaryStr.replace(/^0B/, '');

    // 确保二进制字符串长度是 4 的倍数
    while (binaryStr.length % 4!== 0) {
        binaryStr = '0' + binaryStr;
    }

    const hexChars = '0123456789ABCDEF';
    let hex = '';
    // 每 4 位一组进行处理
    for (let i = 0; i < binaryStr.length; i += 4) {
        const binaryChunk = binaryStr.slice(i, i + 4);
        // 将 4 位二进制数转换为十进制数
        const decimal = parseInt(binaryChunk, 2);
        // 将十进制数转换为十六进制字符
        hex += hexChars[decimal];
    }
    return  hex;
    }


    //% block="RGB 宽度[W]*高度[H] LED点阵模块 [PIN]设置亮度[BRIGHTNESS]" blockType="command"
    //% W.shadow="number"   W.defl="16" 
    //% H.shadow="number"   H.defl="8"  
    //% PIN.shadow="dropdown" PIN.options="PIN_DigitalWrite"  
    //% BRIGHTNESS.shadow="range" BRIGHTNESS.params.min=0 BRIGHTNESS.params.max=255 BRIGHTNESS.defl=50
    export function SetMatrix_BRIGHTNESS(parameter: any, block: any) {
        let w = parameter.W.code;
        let h = parameter.H.code;
        let pin = parameter.PIN.code;
        let brightness = parameter.BRIGHTNESS.code;
        Generator.addInclude('include_Adafruit_GFX', '#include <Adafruit_GFX.h>'); 
        Generator.addInclude('include_RL_NeoMatrix', '#include <RL_NeoMatrix.h>');   
        Generator.addInclude('include_DFRobot_NeoPixel', '#include <DFRobot_NeoPixel.h>');
        Generator.addObject(`object_Adafruit_NeoMatrix_matrix_${w}${h}${pin}`, `Adafruit_NeoMatrix`,  ` matrix_${w}${h}${pin} = Adafruit_NeoMatrix(${w}, ${h}, ${pin},NEO_MATRIX_TOP+ NEO_MATRIX_RIGHT +NEO_MATRIX_ROWS + NEO_MATRIX_PROGRESSIVE,NEO_GRB + NEO_KHZ800);`);
        Generator.addSetup(`setup_matrix_${w}${h}${pin}_begin`, `matrix_${w}${h}${pin}.begin(${pin},${w}*${h});`);
        Generator.addSetup(`setup_matrix_${w}${h}${pin}_setBrightness`, `matrix_${w}${h}${pin}.setBrightness(${brightness});`);

    }

    //% block="RGB 宽度[W]*高度[H] LED点阵模块 [PIN]在X[X]Y[Y]显示[MT]颜色[COLOR]" blockType="command"
    //% W.shadow="number"   W.defl="16" 
    //% H.shadow="number"   H.defl="8"  
    //% PIN.shadow="dropdown" PIN.options="PIN_DigitalWrite" 
    //% X.shadow="number"   X.defl="0" 
    //% Y.shadow="number"   Y.defl="0" 
    //% MT.shadow="matrix" MT.params.row=8 MT.params.column=16 MT.defl="00000000000000000011100000011100001110000001110000111000000111000000000000000000000000000000000000000100001000000000001111000000"
    //% MT.params.builtinFunc="getBuiltinFunc_" 
    //% COLOR.shadow="colorPalette"

    export function SetMatrix_MT816(parameter: any, block: any) {
        let w = parameter.W.code;
        let h = parameter.H.code;
        let pin = parameter.PIN.code;
        let x = parameter.X.code;
        let y = parameter.Y.code;
        let matrix = parameter.MT.code;
        let matrix_num;
        let hz_matrix:any[] = [0,0,0,0,0,0,0,0];
        
        for (let i = 0; i < 16; i++) {

         hz_matrix[i] = binaryToHex(matrix.slice(0+i*8, 8+i*8));
         
        }
        let color=parameter.COLOR.code; //获取传进来的值  
        var r = 0;
        var g = 0;
        var b = 0;
        try {
            if ( color.length == 8 ) {//分别截取RGB值然后转换为数字值
                r = parseInt(color.substring(2, 4), 16);
                g = parseInt(color.substring(4, 6), 16);
                b = parseInt(color.substring(6, 8), 16);
            }
        } catch(e) {
            return '';
        }
        
        Generator.addInclude('include_Adafruit_GFX', '#include <Adafruit_GFX.h>'); 
        Generator.addInclude('include_RL_NeoMatrix', '#include <RL_NeoMatrix.h>');   
        Generator.addInclude('include_DFRobot_NeoPixel', '#include <DFRobot_NeoPixel.h>');
        Generator.addObject(`object_Adafruit_NeoMatrix_matrix_${w}${h}${pin}`, `Adafruit_NeoMatrix`,  ` matrix_${w}${h}${pin} = Adafruit_NeoMatrix(${w}, ${h}, ${pin},NEO_MATRIX_TOP+ NEO_MATRIX_RIGHT +NEO_MATRIX_ROWS + NEO_MATRIX_PROGRESSIVE,NEO_GRB + NEO_KHZ800);`);
        Generator.addSetup(`setup_matrix_${w}${h}${pin}_begin`, `matrix_${w}${h}${pin}.begin(${pin},${w}*${h});`);

        Generator.addInclude(`matrix_${hz_matrix[0]}${hz_matrix[1]}${hz_matrix[2]}${hz_matrix[3]}${hz_matrix[4]}${hz_matrix[5]}${hz_matrix[6]}${hz_matrix[7]}${hz_matrix[8]}${hz_matrix[9]}${hz_matrix[10]}${hz_matrix[11]}${hz_matrix[12]}${hz_matrix[13]}${hz_matrix[14]}${hz_matrix[15]}`, `uint8_t matrix_${hz_matrix[0]}${hz_matrix[1]}${hz_matrix[2]}${hz_matrix[3]}${hz_matrix[4]}${hz_matrix[5]}${hz_matrix[6]}${hz_matrix[7]}${hz_matrix[8]}${hz_matrix[9]}${hz_matrix[10]}${hz_matrix[11]}${hz_matrix[12]}${hz_matrix[13]}${hz_matrix[14]}${hz_matrix[15]}[][16] = {{0x${hz_matrix[0]},0x${hz_matrix[1]},0x${hz_matrix[2]},0x${hz_matrix[3]},0x${hz_matrix[4]},0x${hz_matrix[5]},0x${hz_matrix[6]},0x${hz_matrix[7]},0x${hz_matrix[8]},0x${hz_matrix[9]},0x${hz_matrix[10]},0x${hz_matrix[11]},0x${hz_matrix[12]},0x${hz_matrix[13]},0x${hz_matrix[14]},0x${hz_matrix[15]}}};`);
        Generator.addCode(`matrix_${w}${h}${pin}.fillScreen(matrix_${w}${h}${pin}.Color(0,0,0));`);
        Generator.addCode(`matrix_${w}${h}${pin}.drawBitmap(${x}, ${y}, matrix_${hz_matrix[0]}${hz_matrix[1]}${hz_matrix[2]}${hz_matrix[3]}${hz_matrix[4]}${hz_matrix[5]}${hz_matrix[6]}${hz_matrix[7]}${hz_matrix[8]}${hz_matrix[9]}${hz_matrix[10]}${hz_matrix[11]}${hz_matrix[12]}${hz_matrix[13]}${hz_matrix[14]}${hz_matrix[15]}[0], 16, 8, matrix_${w}${h}${pin}.Color(${r}, ${g}, ${b}));`);
        Generator.addCode(`matrix_${w}${h}${pin}.show();`);
    }
    //% block="RGB 宽度[W]*高度[H] LED点阵模块 [PIN]在X[X]Y[Y]显示[MT]颜色[COLOR]" blockType="command"
    //% W.shadow="number"   W.defl="16" 
    //% H.shadow="number"   H.defl="8"  
    //% PIN.shadow="dropdown" PIN.options="PIN_DigitalWrite" 
    //% X.shadow="number"   X.defl="0" 
    //% Y.shadow="number"   Y.defl="0" 
    //% MT.shadow="matrix" MT.params.row=8 MT.params.column=8 MT.defl="0011110001000010101001011000000110100101100110010100001000111100"
    //% MT.params.builtinFunc="getBuiltinFunc_88" 
    //% COLOR.shadow="colorPalette"

    export function SetMatrix_MT88(parameter: any, block: any) {
        let w = parameter.W.code;
        let h = parameter.H.code;
        let pin = parameter.PIN.code;
        let x = parameter.X.code;
        let y = parameter.Y.code;
        let matrix = parameter.MT.code;
        let matrix_num;
        let hz_matrix:any[] = [0,0,0,0,0,0,0,0];
        
        for (let i = 0; i < 8; i++) {

         hz_matrix[i] = binaryToHex(matrix.slice(0+i*8, 8+i*8));
         
        }
        let color=parameter.COLOR.code; //获取传进来的值  
        var r = 0;
        var g = 0;
        var b = 0;
        try {
            if ( color.length == 8 ) {//分别截取RGB值然后转换为数字值
                r = parseInt(color.substring(2, 4), 16);
                g = parseInt(color.substring(4, 6), 16);
                b = parseInt(color.substring(6, 8), 16);
            }
        } catch(e) {
            return '';
        }
        
        Generator.addInclude('include_Adafruit_GFX', '#include <Adafruit_GFX.h>'); 
        Generator.addInclude('include_RL_NeoMatrix', '#include <RL_NeoMatrix.h>');   
        Generator.addInclude('include_DFRobot_NeoPixel', '#include <DFRobot_NeoPixel.h>');
        Generator.addObject(`object_Adafruit_NeoMatrix_matrix_${w}${h}${pin}`, `Adafruit_NeoMatrix`,  ` matrix_${w}${h}${pin} = Adafruit_NeoMatrix(${w}, ${h}, ${pin},NEO_MATRIX_TOP+ NEO_MATRIX_RIGHT +NEO_MATRIX_ROWS + NEO_MATRIX_PROGRESSIVE,NEO_GRB + NEO_KHZ800);`);
        Generator.addSetup(`setup_matrix_${w}${h}${pin}_begin`, `matrix_${w}${h}${pin}.begin(${pin},${w}*${h});`);

        Generator.addInclude(`matrix_${hz_matrix[0]}${hz_matrix[1]}${hz_matrix[2]}${hz_matrix[3]}${hz_matrix[4]}${hz_matrix[5]}${hz_matrix[6]}${hz_matrix[7]}`, `uint8_t matrix_${hz_matrix[0]}${hz_matrix[1]}${hz_matrix[2]}${hz_matrix[3]}${hz_matrix[4]}${hz_matrix[5]}${hz_matrix[6]}${hz_matrix[7]}[][8] = {{0x${hz_matrix[0]},0x${hz_matrix[1]},0x${hz_matrix[2]},0x${hz_matrix[3]},0x${hz_matrix[4]},0x${hz_matrix[5]},0x${hz_matrix[6]},0x${hz_matrix[7]}}};`);
        Generator.addCode(`matrix_${w}${h}${pin}.fillScreen(matrix_${w}${h}${pin}.Color(0,0,0));`);
        Generator.addCode(`matrix_${w}${h}${pin}.drawBitmap(${x}, ${y}, matrix_${hz_matrix[0]}${hz_matrix[1]}${hz_matrix[2]}${hz_matrix[3]}${hz_matrix[4]}${hz_matrix[5]}${hz_matrix[6]}${hz_matrix[7]}[0], 8, 8, matrix_${w}${h}${pin}.Color(${r}, ${g}, ${b}));`);
        Generator.addCode(`matrix_${w}${h}${pin}.show();`);
    }
        function binaryToHex2(binaryStr) {
    // 去掉 "0B" 前缀
    binaryStr = binaryStr.replace(/^0B/, '');

    // 确保二进制字符串长度是 4 的倍数
    while (binaryStr.length % 4!== 0) {
        binaryStr =  binaryStr+ '0' ;
    }

    const hexChars = '0123456789ABCDEF';
    let hex = '';
    // 每 4 位一组进行处理
    for (let i = 0; i < binaryStr.length; i += 4) {
        const binaryChunk = binaryStr.slice(i, i + 4);
        // 将 4 位二进制数转换为十进制数
        const decimal = parseInt(binaryChunk, 2);
        // 将十进制数转换为十六进制字符
        hex += hexChars[decimal];
    }
    return  hex;
    }
    //% block="RGB 宽度[W]*高度[H] LED点阵模块 [PIN]在X[X]Y[Y]显示[MT]颜色[COLOR]" blockType="command"
    //% W.shadow="number"   W.defl="16" 
    //% H.shadow="number"   H.defl="8"  
    //% PIN.shadow="dropdown" PIN.options="PIN_DigitalWrite" 
    //% X.shadow="number"   X.defl="0" 
    //% Y.shadow="number"   Y.defl="0" 
    //% MT.shadow="matrix" MT.params.row=5 MT.params.column=5 MT.defl="0101010101100010101000100"
    //% COLOR.shadow="colorPalette"

    export function SetMatrix_MT55(parameter: any, block: any) {
        let w = parameter.W.code;
        let h = parameter.H.code;
        let pin = parameter.PIN.code;
        let x = parameter.X.code;
        let y = parameter.Y.code;
        let matrix = parameter.MT.code;
        let matrix_num;
        let hz_matrix:any[] = [0,0,0,0,0];
        
        for (let i = 0; i < 5; i++) {

         hz_matrix[i] = binaryToHex2(matrix.slice(0+i*5, 5+i*5));
         
        }
        let color=parameter.COLOR.code; //获取传进来的值  
        var r = 0;
        var g = 0;
        var b = 0;
        try {
            if ( color.length == 8 ) {//分别截取RGB值然后转换为数字值
                r = parseInt(color.substring(2, 4), 16);
                g = parseInt(color.substring(4, 6), 16);
                b = parseInt(color.substring(6, 8), 16);
            }
        } catch(e) {
            return '';
        }
        
        Generator.addInclude('include_Adafruit_GFX', '#include <Adafruit_GFX.h>'); 
        Generator.addInclude('include_RL_NeoMatrix', '#include <RL_NeoMatrix.h>');   
        Generator.addInclude('include_DFRobot_NeoPixel', '#include <DFRobot_NeoPixel.h>');
        Generator.addObject(`object_Adafruit_NeoMatrix_matrix_${w}${h}${pin}`, `Adafruit_NeoMatrix`,  ` matrix_${w}${h}${pin} = Adafruit_NeoMatrix(${w}, ${h}, ${pin},NEO_MATRIX_TOP+ NEO_MATRIX_RIGHT +NEO_MATRIX_ROWS + NEO_MATRIX_PROGRESSIVE,NEO_GRB + NEO_KHZ800);`);
        Generator.addSetup(`setup_matrix_${w}${h}${pin}_begin`, `matrix_${w}${h}${pin}.begin(${pin},${w}*${h});`);

        Generator.addInclude(`matrix_${hz_matrix[0]}${hz_matrix[1]}${hz_matrix[2]}${hz_matrix[3]}${hz_matrix[4]}`, `uint8_t matrix_${hz_matrix[0]}${hz_matrix[1]}${hz_matrix[2]}${hz_matrix[3]}${hz_matrix[4]}[][5] = {{0x${hz_matrix[0]},0x${hz_matrix[1]},0x${hz_matrix[2]},0x${hz_matrix[3]},0x${hz_matrix[4]}}};`);
        Generator.addCode(`matrix_${w}${h}${pin}.fillScreen(matrix_${w}${h}${pin}.Color(0,0,0));`);
        Generator.addCode(`matrix_${w}${h}${pin}.drawBitmap(${x}, ${y}, matrix_${hz_matrix[0]}${hz_matrix[1]}${hz_matrix[2]}${hz_matrix[3]}${hz_matrix[4]}[0], 5, 5, matrix_${w}${h}${pin}.Color(${r}, ${g}, ${b}));`);
        Generator.addCode(`matrix_${w}${h}${pin}.show();`);
    }
    //% block="RGB 宽度[W]*高度[H] LED点阵模块[PIN] [STR]颜色[COLOR]" blockType="command"
    //% W.shadow="number"   W.defl="16" 
    //% H.shadow="number"   H.defl="8" 
    //% PIN.shadow="dropdown" PIN.options="PIN_DigitalWrite" 
    //% STR.shadow="string" STR.defl="hello ruilong!"
    //% COLOR.shadow="colorPalette"
    //% BRIGHTNESS.shadow="range" BRIGHTNESS.params.min=0 BRIGHTNESS.params.max=255 BRIGHTNESS.defl=50
    export function SetMatrix_STR(parameter: any, block: any) {
        let w = parameter.W.code;
        let h = parameter.H.code;
        let pin = parameter.PIN.code;
        let str = parameter.STR.code;
        let length = str.length;
        let color=parameter.COLOR.code; //获取传进来的值  
        var r = 0;
        var g = 0;
        var b = 0;
        try {
            if ( color.length == 8 ) {//分别截取RGB值然后转换为数字值
                r = parseInt(color.substring(2, 4), 16);
                g = parseInt(color.substring(4, 6), 16);
                b = parseInt(color.substring(6, 8), 16);
            }
        } catch(e) {
            return '';
        }

        Generator.addInclude('include_Adafruit_GFX', '#include <Adafruit_GFX.h>'); 
        Generator.addInclude('include_RL_NeoMatrix', '#include <RL_NeoMatrix.h>');   
        Generator.addInclude('include_DFRobot_NeoPixel', '#include <DFRobot_NeoPixel.h>');
        Generator.addObject(`object_Adafruit_NeoMatrix_matrix_${w}${h}${pin}`, `Adafruit_NeoMatrix`,  ` matrix_${w}${h}${pin} = Adafruit_NeoMatrix(${w}, ${h}, ${pin},NEO_MATRIX_TOP+ NEO_MATRIX_RIGHT +NEO_MATRIX_ROWS + NEO_MATRIX_PROGRESSIVE,NEO_GRB + NEO_KHZ800);`);
        Generator.addSetup(`setup_matrix_${w}${h}${pin}_begin`, `matrix_${w}${h}${pin}.begin(${pin},${w}*${h});`);

        Generator.addSetup(`setup_matrix_${w}${h}${pin}_setTextWrap`, `matrix_${w}${h}${pin}.setTextWrap(false);`);
        Generator.addCode(`matrix_${w}${h}${pin}.scrollDisplay(${str}, ${length}*5+2,matrix_${w}${h}${pin}.Color(${r}, ${g}, ${b}));`);

    }
    //% block="RGB 宽度[W]*高度[H] LED点阵模块 [PIN]设置X[X]Y[Y]颜色[COLOR]" blockType="command"
    //% W.shadow="number"   W.defl="16" 
    //% H.shadow="number"   H.defl="8" 
    //% PIN.shadow="dropdown" PIN.options="PIN_DigitalWrite" 
    //% X.shadow="number"   X.defl="0" 
    //% Y.shadow="number"   Y.defl="0" 
    //% COLOR.shadow="colorPalette"
    export function SetMatrix_PIXEL(parameter: any, block: any) {
        let w = parameter.W.code;
        let h = parameter.H.code;
        let pin = parameter.PIN.code;
        let x = parameter.X.code;
        let y = parameter.Y.code;
        let color=parameter.COLOR.code; //获取传进来的值  
        var r = 0;
        var g = 0;
        var b = 0;
        try {
            if ( color.length == 8 ) {//分别截取RGB值然后转换为数字值
                r = parseInt(color.substring(2, 4), 16);
                g = parseInt(color.substring(4, 6), 16);
                b = parseInt(color.substring(6, 8), 16);
            }
        } catch(e) {
            return '';
        }
        
        Generator.addInclude('include_Adafruit_GFX', '#include <Adafruit_GFX.h>'); 
        Generator.addInclude('include_RL_NeoMatrix', '#include <RL_NeoMatrix.h>');   
        Generator.addInclude('include_DFRobot_NeoPixel', '#include <DFRobot_NeoPixel.h>');
        Generator.addObject(`object_Adafruit_NeoMatrix_matrix_${w}${h}${pin}`, `Adafruit_NeoMatrix`,  ` matrix_${w}${h}${pin} = Adafruit_NeoMatrix(${w}, ${h}, ${pin},NEO_MATRIX_TOP+ NEO_MATRIX_RIGHT +NEO_MATRIX_ROWS + NEO_MATRIX_PROGRESSIVE,NEO_GRB + NEO_KHZ800);`);
        Generator.addSetup(`setup_matrix_${w}${h}${pin}_begin`, `matrix_${w}${h}${pin}.begin(${pin},${w}*${h});`);

        Generator.addCode(`matrix_${w}${h}${pin}.drawPixel(${w}-${x}-1,${y},matrix_${w}${h}${pin}.Color(${r}, ${g}, ${b}));`);
        Generator.addCode(`matrix_${w}${h}${pin}.show();`);

    }

    //% block="RGB 宽度[W]*高度[H] LED点阵模块 [PIN]填充全部 颜色[COLOR]" blockType="command"
    //% W.shadow="number"   W.defl="16" 
    //% H.shadow="number"   H.defl="8" 
    //% PIN.shadow="dropdown" PIN.options="PIN_DigitalWrite" 
    //% COLOR.shadow="colorPalette"
    export function SetMatrix_FILLSCREEM(parameter: any, block: any) {
        let w = parameter.W.code;
        let h = parameter.H.code;
        let pin = parameter.PIN.code;
        let color=parameter.COLOR.code; //获取传进来的值  
        var r = 0;
        var g = 0;
        var b = 0;
        try {
            if ( color.length == 8 ) {//分别截取RGB值然后转换为数字值
                r = parseInt(color.substring(2, 4), 16);
                g = parseInt(color.substring(4, 6), 16);
                b = parseInt(color.substring(6, 8), 16);
            }
        } catch(e) {
            return '';
        }

        Generator.addInclude('include_Adafruit_GFX', '#include <Adafruit_GFX.h>'); 
        Generator.addInclude('include_RL_NeoMatrix', '#include <RL_NeoMatrix.h>');   
        Generator.addInclude('include_DFRobot_NeoPixel', '#include <DFRobot_NeoPixel.h>');
        Generator.addObject(`object_Adafruit_NeoMatrix_matrix_${w}${h}${pin}`, `Adafruit_NeoMatrix`,  ` matrix_${w}${h}${pin}= Adafruit_NeoMatrix(${w}, ${h}, ${pin},NEO_MATRIX_TOP+ NEO_MATRIX_RIGHT +NEO_MATRIX_ROWS + NEO_MATRIX_PROGRESSIVE,NEO_GRB + NEO_KHZ800);`);
        Generator.addSetup(`setup_matrix_${w}${h}${pin}_begin`, `matrix_${w}${h}${pin}.begin(${pin},${w}*${h});`);

        Generator.addCode(`matrix_${w}${h}${pin}.fillScreen(matrix_${w}${h}${pin}.Color(${r}, ${g}, ${b}));`);  
        Generator.addCode(`matrix_${w}${h}${pin}.show();`);

    }


   
}
